package cn.userManager.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.userManager.po.User;
import cn.userManager.service.UserService;
@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@RequestMapping("/register")
  public void register(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception
  {
		User user=new User();
		user.setMobilephone(request.getParameter("member_mobile"));
		 String member_name=request.getParameter("member_name");
		user.setUseraccount(member_name);
		user.setUserpassword(request.getParameter("password"));
		String member_role_type=request.getParameter("member_role_type");
		user.setUsertype(member_role_type);
		user.setStatus("1");
		userService.insertUser(user);
		
  }
@RequestMapping(value = "/login", method = { RequestMethod.POST,RequestMethod.GET })
  public String login(HttpServletRequest request,HttpSession session) throws Exception
  {
	String username=request.getParameter("member_name");
	String password=request.getParameter("password");
	User user1=userService.selectByName(username);
	User user2=userService.selectByPhone(username);
	if(user1!=null&&user1.getUserpassword().equals(password))
	{
		session.setAttribute("useraccount",user1.getUseraccount());
		 return "redirect:htm/base/index.html";
	}
	if(user2!=null&&user2.getUserpassword().equals(password))
	{
		session.setAttribute("useraccount",user1.getUseraccount());
		 return "redirect:htm/base/index.html";
	}
    return "redirect:htm/userManager/login_fail.html";
  }
@RequestMapping(value = "/getUser", method = { RequestMethod.POST,RequestMethod.GET })
public @ResponseBody List<String> getUser(HttpServletRequest request,HttpSession session) throws Exception
{
    String member_name=(String) session.getAttribute("useraccount");
	ArrayList<String> list=new ArrayList<String>();
	list.add(member_name);
	return list;
}

}
